/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ma.projet.managedBean;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import ma.projet.entities.Incident;
import ma.projet.local.IncidentFacadeLocal;

/**
 *
 * @author Maison
 */
@ManagedBean
@RequestScoped
public class IncidentBean {

    @EJB
    private IncidentFacadeLocal ifl;
    private Incident incident;
    private List<Incident> incidents;

    public Incident getIncident() {
        return incident;
    }

    public void setIncident(Incident incident) {
        this.incident = incident;
    }

    public void setIncidents(List<Incident> incidents) {
        this.incidents = incidents;
    }

    /**
     * Creates a new instance of IncidentBean
     */
    public IncidentBean() {
    }

    public String conv_date(Date date) {
        String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        return sdf.format(date);
    }

    public List<Incident> getinc() {
        getIncidents();
        List<Incident> in = incidents;

        Collections.sort(in, new Comparator<Incident>() {
            @Override
            public int compare(Incident o1, Incident o2) {
                return o1.getNincident().compareTo(o2.getNincident());
            }
        });
        Collections.reverse(in);
        return in;

    }

    public List<Incident> getIncidents() {
        if (incidents == null) {
            incidents = ifl.findAll();
        }
        return incidents;
    }

    public String Week(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
        return "W" + c.get(Calendar.WEEK_OF_YEAR);
    }

}
